package com.xingyue.loginqueryarticle.utils;

import java.sql.*;

/**
 * Created by hdwx on 2016/11/19.
 */
public final class JDBCUtils {


    static final String URL = "jdbc:mysql://localhost:3306/test2";
    static final String USER = "root";
    static final String PASWD = "yuedi";

    //改类不让继承,因为是工具类
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //抛出异常
    public static Connection getConnection() throws SQLException {
        //连接数据库
        return DriverManager.getConnection(URL,USER,PASWD);
    }

    //封装  执行sql语句方法                                           //注入sql语句 ,是否返回自增ID
    public static PreparedStatement getPreparedStatement(String sql, boolean isReturnId) throws SQLException{
        int i = isReturnId? Statement.RETURN_GENERATED_KEYS:Statement.NO_GENERATED_KEYS;

        //  : [PrearepedStatement stm] = con.prepareStatement(sql,自增列);预处理 getConnection= con
        return getConnection().prepareStatement(sql, i);
    }
}
